<?php
class installDomain{
	var $cPanelUser;
	var $cPanelPass;
	var $rootDomain;
	function __construct(){
		$this->cPanelUser = "danatrad";
		$this->cPanelPass = "dana123$%^";
		$this->rootDomain = "danatrade.vn";
	}
	function create_subdomain($subDomain) {
 
		//  $buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain;

		$buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $this->rootDomain . "&domain=" . $subDomain . "&dir=public_html/shop/" . $subDomain;

		$openSocket = fsockopen('localhost',2082);
		if(!$openSocket) {
		    return "Socket error";
		    exit();
		}

		$authString = $this->cPanelUser . ":" . $this->cPanelPass;
		$authPass = base64_encode($authString);
		$buildHeaders  = "GET " . $buildRequest ."\r\n";
		$buildHeaders .= "HTTP/1.0\r\n";
		$buildHeaders .= "Host:localhost\r\n";
		$buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
		$buildHeaders .= "\r\n";

		fputs($openSocket, $buildHeaders);
		while(!feof($openSocket)) {
			fgets($openSocket,128);
		}
		fclose($openSocket);

		$newDomain = "http://" . $subDomain . "." . $this->rootDomain . "/";

		//  return "Created subdomain $newDomain";

	}
	function delete_subdomain($subDomain){
		$buildRequest = "/frontend/x3/subdomain/dodeldomain.html?domain=" . $subDomain . "_" . $this->rootDomain;

		$openSocket = fsockopen('localhost',2082);
		if(!$openSocket) {
		    return "Socket error";
		    exit();
		}

		$authString = $this->cPanelUser . ":" . $this->cPanelPass;
		$authPass = base64_encode($authString);
		$buildHeaders  = "GET " . $buildRequest ."\r\n";
		$buildHeaders .= "HTTP/1.0\r\n";
		$buildHeaders .= "Host:localhost\r\n";
		$buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
		$buildHeaders .= "\r\n";

		fputs($openSocket, $buildHeaders);
		while(!feof($openSocket)) {
		fgets($openSocket,128);
		}
		fclose($openSocket);
		 
		$passToShell = "rm -rf /home/" . $cPanelUser . "/public_html/subdomains/" . $subDomain;
		system($passToShell);
	}
}
//$crDomain = new installDomain();
//if(!empty($_GET['domainname']))
//	$crDomain->create_subdomain($_GET['domainname']);
//$crDomain->delete_subdomain('loipn123456789');
?>